// @author U099151W Sun Chenchen
// all functions in this file  are implemented by the author unless specifically indicated

debugMode = false;
var InputManager = function() {
    var _player;
    var _canvas;
    var _camera;

    var _mouse = {
        x : 0,
        y : 0,
        press :false
    };

    var _keys = [];// physical key states
    var K_LEFT = 37;
    var K_RIGHT = 39;
    var K_UP = 38;
    var K_DOWN = 40;
    var K_CTRL = 17;
    var K_SPACE = 32;
    var K_ENTER = 13;
    var K_W = 87;
    var K_A = 65;
    var K_D = 68;
    var K_S = 83;
    var K_CAPS = 20;
    var K_1 = 49;
    var K_2 = 50;
    var K_3 = 51;
    var K_4 = 52;

    var COUNT;
    var DELAY_RATE = 2;

    this.init = function(args) {
        _canvas = document.getElementById(args.canvas);
        _player = args.player;
        _camera = args.camera;

        _keys = [];
        for (var i=0; i<255; i++) {
            var newKey = {press : false};
            _keys.push(newKey);
        }

        window.addEventListener('keydown', 	 this.handleKeyDown, true );
        window.addEventListener('keyup', 	 this.handleKeyUp, true );
        /*
         window.addEventListener("mousedown", this.clickInCanvas, true );
         window.addEventListener("mouseup", 	 this.releaseInCanvas, true );
         window.addEventListener("mousemove", this.moveInCanvas, true );*/

    };

    /////////////////////////////////////////////////////////
    // User-Event interface
    this.clickInCanvas = function( e )
    {
        var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

        x -= _canvas.offsetLeft;
        y -= _canvas.offsetTop;

        if( x >= 0 && y >= 0
            && x < _canvas.width
            && y < _canvas.height )
        {
            _mouse.press = true ;
        }
    };

    this.releaseInCanvas = function( e )
    {
        var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

        x -= _canvas.offsetLeft;
        y -= _canvas.offsetTop;

        if( x >= 0 && y >= 0
            && x < _canvas.width
            && y < _canvas.height )
        {
            _mouse.press = false ;
        }
    };

    this.moveInCanvas = function( e )
    {
        var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

        x -= _canvas.offsetLeft;
        y -= _canvas.offsetTop;

        if( x >= 0 && y >= 0
            && x < _canvas.width
            && y < _canvas.height )
        {
            _mouse.x = x ;
            _mouse.y = y ;
        }
    };

    this.handleKeyDown = function( evt )
    {
        //this.keys[evt.keyCode] = this.keys[evt.keyCode];
        _keys[evt.keyCode].press = true;
    };
    this.handleKeyUp = function( evt )
    {
        //this.keys[evt.keyCode] = this.keys[evt.keyCode];
        _keys[evt.keyCode].press = false;
    };
    this.processEntry = function() {
        if (_keys[K_W].press) {
            _player.walk("Up", _camera.updateCameraBasedOnPlayer);
        }
        if (_keys[K_A].press) {
            _player.walk("Left", _camera.updateCameraBasedOnPlayer);
        }
        if (_keys[K_S].press) {
            _player.walk("Down", _camera.updateCameraBasedOnPlayer);
        }
        if (_keys[K_D].press) {
            _player.walk("Right", _camera.updateCameraBasedOnPlayer);
        }
        if (_keys[K_2].press) {
            debugMode = true;
        }
        if (_keys[K_1].press) {
            debugMode = false;
        }
    }
};
var inputManager = new InputManager();